package com.qy.controller.manage;
import com.qy.base.core.Constants;
import com.qy.base.core.Result;
import com.qy.base.core.ResultGenerator;
import com.qy.model.Users;
import com.qy.service.UsersService;
import com.qy.base.core.PageBean;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**Created
* Created by sxd on 2018/09/16.
*/
@RestController
@RequestMapping("/users")
public class UsersController {
    @Resource
    private UsersService usersService;

    /**
     * 用户注册
     * @param users
     *      * @return
     */
    @PostMapping("/add")
    public Result add(Users users) {
        int num = usersService.SelectCount(users.getEmail());
        if (num==1){
            return ResultGenerator.errResult(Constants.CODE_ERR_USER_REPEAT);
        }
        usersService.save(users);
        return ResultGenerator.successResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Integer id) {
        usersService.deleteById(id);
        return ResultGenerator.successResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Users users) {
        usersService.update(users);
        return ResultGenerator.successResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Users users = usersService.findById(id);
        return ResultGenerator.successResult(users);
    }

    @GetMapping("/list")
    public Result list(PageBean<Users> page) {
        PageHelper.startPage(page.getPageNum(),page.getSize());
        List<Users> list = usersService.findAll();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    /**
     * 登录验证
     * @param users
     * @return boolean
     */
    @PostMapping("check_login")
    public boolean check_login(Users users){
        int num = usersService.SelectCount(users.getEmail());
        if (num==0){
            return false;
        }
        String password = usersService.SelectPassword(users.getEmail());
        if (password.equals(users.getPassword())){
            return true;
        }
        return false;
    }

    /**
     * 跳转register.html
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public ModelAndView register(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("register");

        return mav;

    }

    /**
     * 跳转login.html
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ModelAndView login(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("login");

        return mav;

    }



}
